package com.eco.fanliapp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.AppUser;
import com.eco.fanliapp.entity.AppUserSearchHistory;
import com.eco.fanliapp.entity.SyUserTask;
import com.eco.fanliapp.service.*;
import com.eco.fanliapp.vo.respone.ResponeResults;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class UserTaskServiceImpl implements UserTaskService {

    private static final Logger log = LoggerFactory.getLogger(UserTaskServiceImpl.class);

    @Autowired
    private IAppUserSearchHistoryService appUserSearchHistoryService;
    @Autowired
    private ISyUserTaskService syUserTaskService;

    @Autowired
    private UpdateUserCommon updateUserCommon;
    @Autowired
    private IAppUserService appUserService;

    @Async
    public void UpdateUserTask(AppUser u, ServerResponse<ResponeResults> response){
        try {
            if (u.getUserId() != null && "a".equals(response.getData().getPopType())) {//是精准搜索

                Long userId = u.getUserId();

                String itemId = response.getData().getData().get(0).getItemId();

                QueryWrapper<AppUserSearchHistory> searchHistoryQueryWrapper = new QueryWrapper<AppUserSearchHistory>();
                searchHistoryQueryWrapper.eq("user_id", userId).eq("item_id", itemId);
                int count = appUserSearchHistoryService.count(searchHistoryQueryWrapper);
                AppUserSearchHistory appUserSearchHistory = new AppUserSearchHistory();
                if (count <= 0) {
                    appUserSearchHistory.setUserId(userId);
                    appUserSearchHistory.setSearchCount(1);
                    appUserSearchHistory.setItemId(Long.parseLong(itemId));
                    appUserSearchHistoryService.save(appUserSearchHistory);
                }
                //updateUserCommon.UpdateUserMoney(new BigDecimal(0.36), userId, 1);
                QueryWrapper<SyUserTask> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("user_id", userId);
                queryWrapper.eq("task_type_id", 1);

                List<SyUserTask> syUserTasks = syUserTaskService.list(queryWrapper);
                int con=0;
                if (syUserTasks == null || syUserTasks.size() < 1) {
                    con=updateUserCommon.UpdateUserMoney(new BigDecimal(0.36), userId,1);
                    //插入新手任务-复制链接任务日志
                    //插入后，修改状态，并给用户加钱
                }else {
                    SyUserTask syUserTask = syUserTasks.get(0);
                    //如果没有完成新手任务-复制链接任务日志
                    if (syUserTask.getTaskStatus() == null || syUserTask.getTaskStatus() != 5) {
                        //修改状态，并给用户加钱
                        con=updateUserCommon.UpdateUserMoney(new BigDecimal(0.36), userId, 1);

                    }
                }
                /*if(con>0){
                    AppUser appUser = appUserService.selectAppUserByUserId(userId);
                    if (appUser.getSuperInviteCode() != null) {
                        //存在上级
                        AppUser appUserSuper = appUserService.selectSuperUserInfoBySuperInviteCode(appUser.getSuperInviteCode());

                        if (appUserSuper != null) {
                            Long userIdSuper = appUserSuper.getUserId();
                            QueryWrapper<SyUserTask> queryWrapperSuper = new QueryWrapper<>();
                            queryWrapperSuper.eq("user_id", userIdSuper);
                            queryWrapperSuper.eq("task_type_id", 11);
                            List<SyUserTask> syUserTasksSuper = syUserTaskService.list(queryWrapperSuper);
                            //这里给上级加钱
                            if (syUserTasksSuper == null || syUserTasksSuper.size() < 1) {
//							iSyUserMoneyService.usetTaskSuccessUpdateGold(11, userIdSuper);
                            }
                        }
                    }
                }*/
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
